// 导入db模块
const db = require('../common/db/index')

// 定义考试类
class Exam {
    // 考试分类部分......................................
    // 增
    // 新增考试分类名称
    category_add(text, icon) {
        return new Promise((reslove, reject) => {
            // 查询是否重复名称
            db.query(`SELECT * FROM pre_exam_category WHERE text LIKE '${text}' ORDER BY text DESC`)
                .then(
                    row => {
                        if (row.length == 0) {
                            db.query(`insert into pre_exam_category(text,icon) values('${text}','${icon}')`)
                                .then(
                                    content => {
                                        reslove({
                                            code: 200,
                                            msg: "新增数据",
                                            status: content.changedRows == 0 ? "正常" : "异常"
                                        })
                                    }
                                )
                                .catch(
                                    err => {
                                        reject({
                                            code: -1,
                                            msg: "新增失败",
                                            err
                                        })
                                    }
                                )
                        }
                        else {
                            reslove({
                                cede: 201,
                                msg: "这个名称已经存在"
                            })
                        }
                    }
                )

        })
    }
    // 删
    // 删除考试分类名称
    category_remove(id) {
        return new Promise((reslove, reject) => {
            // 查询是否存在名称
            db.query(`SELECT * FROM pre_exam_category WHERE id = ${id}`)
                .then(
                    row => {
                        if (row.length == 0) {
                            reslove({
                                coed: 404,
                                msg: "这个分类不存在"
                            })
                        }
                        else {
                            // 执行删除语句
                            db.query(`DELETE FROM pre_exam_category WHERE pre_exam_category.id = ${id}`)
                                .then(
                                    content => {
                                        reslove({
                                            code: 200,
                                            msg: "删除分类成功",
                                            content
                                        })
                                    }
                                )
                                .catch(
                                    err => {
                                        reject({
                                            code: -1,
                                            msg: "删除失败",
                                            err
                                        })

                                    }
                                )
                        }
                    }
                )
        })
    }
    // 改
    // 修改考试分类名称
    category_modify(id, text) {
        return new Promise((reslove, reject) => {
            db.query(`SELECT * FROM pre_exam_category WHERE id = ${id}`)
                .then(
                    row => {
                        if (row.length == 0) {
                            reslove({
                                code: 404,
                                msg: "这个分类不存在"
                            })
                        }
                        else {
                            db.query(`update pre_exam_category set text= '${text}' where id = ${id}`)
                                .then(
                                    content => {
                                        reslove({
                                            code: 200,
                                            msg: "修改分类名称成功",
                                            status: content.fieldCount == 0 ? '正常' : '异常'
                                        })
                                    }
                                )
                                .catch(
                                    err => {
                                        reject({
                                            code: -1,
                                            msg: "修改失败",
                                            err
                                        })
                                    }
                                )
                        }
                    }
                )
        })
    }
    // 查
    // 查考试分类列表
    category_list() {
        return new Promise((resolve, reject) => {
            db.query('SELECT * FROM `pre_exam_category`;')
                .then(
                    row => {
                        resolve({
                            code: 200,
                            msg: "获取考试分类列表成功",
                            result: row
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "获取考试分类列表失败",
                            err
                        })
                    }
                )
        })
    }

    // 试卷部分.................................................
    // 新增试卷
    exam_add(id, text, name, total, passline, time) {
        return new Promise((reslove, reject) => {
            // 查询是否重复名称
            db.query(`SELECT * FROM pre_exam_list WHERE name LIKE '${name}' ORDER BY text DESC`)
                .then(
                    row => {
                        if (row.length == 0) {
                            db.query(`INSERT INTO pre_exam_list (id,text,name,total,passline,time) VALUES ('${id}','${text}','${name}','${total}','${passline}','${time}');`)
                                .then(
                                    content => {
                                        reslove({
                                            code: 200,
                                            msg: "新增试卷成功",
                                            status: content.changedRows == 0 ? "正常" : "异常"
                                        })
                                    }
                                )
                                .catch(
                                    err => {
                                        reject({
                                            code: -1,
                                            msg: "新增失败",
                                            err
                                        })
                                    }
                                )
                        }
                        else {
                            reslove({
                                cede: 201,
                                msg: "这个试卷已经存在"
                            })
                        }
                    }
                )

        })
    }

    // 查找试卷根据eid查找试卷
    exam_list(eid) {
        return new Promise((resolve, reject) => {
            db.query(`SELECT * FROM pre_exam_list where eid = '${eid}';`)
                .then(
                    row => {
                        resolve({
                            code: 200,
                            msg: "获取试卷列表成功",
                            result: row
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "获取试卷列表失败",
                            err
                        })
                    }
                )
        })
    }
    // 获取所有试卷列表
    exam_list_all() {
        return new Promise((resolve, reject) => {
            db.query(`SELECT * FROM pre_exam_list where eid;`)
                .then(
                    row => {
                        resolve({
                            code: 200,
                            msg: "获取试卷列表成功",
                            result: row
                        })
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "获取试卷列表失败",
                            err
                        })
                    }
                )
        })
    }
    // 删除试卷列表
    exam_delete(eid) {
        return new Promise((resolve, reject) => {
            db.query(`SELECT * FROM pre_exam_list where eid = '${eid}';`)
                .then(rows => {
                    if (rows.length == 0) {
                        reject({
                            code: 404,
                            msg: "该试卷不存在"
                        })
                    }
                    else {
                        db.query(`DELETE FROM pre_exam_list WHERE pre_exam_list.eid = ${eid}`)
                            .then(content => {
                                resolve({
                                    code: 200,
                                    msg: "删除成功",
                                    content
                                }, err => {
                                    reject({
                                        code: -1,
                                        msg: "删除失败",
                                        err
                                    })
                                })
                            })
                    }
                })
        })
    }
    // 编辑试卷信息
    exam_edit(eid, id, text, name, total, passline, time) {
        return new Promise((reslove, reject) => {
            db.query(`SELECT * FROM pre_exam_list where eid = '${eid}';`)
                .then(rows => {
                    if (rows.length == 0) {
                        reject({
                            code: 404,
                            msg: "这个试卷不存在"
                        })
                    }
                    else {
                        db.query(`update pre_exam_list set id ='${id}',text='${text}' ,name='${name}',total='${total}',passline='${passline}',time='${time}' where eid='${eid}'`)
                            .then(content => {
                                reslove({
                                    code: 200,
                                    msg: "编辑成功~",
                                    content
                                })
                            }, err => {
                                reject({
                                    code: -1,
                                    msg: "编辑失败~",
                                    err
                                })
                            })
                    }
                })
        })
    }

    // 题目部分................................................
    // 新增题目列表
    topic_add(eid, name, question, answer, score, option) {
        return new Promise((reslove, reject) => {
            // 查询是否重复名称
            db.query(`SELECT question FROM pre_exam_topic WHERE question LIKE '${question}'`)
                .then(
                    row => {
                        if (row.length == 0) {
                            // 得到满分
                            db.query(`SELECT total FROM pre_exam_list WHERE eid = '${eid}';`)
                                .then(content => {
                                    // content[0].total 满分
                                    // 计算总分
                                    db.query(`SELECT SUM(score) AS total FROM pre_exam_topic WHERE eid = '${eid}'`)
                                        .then(rows => {
                                            // rows[0].total 总分
                                            // 计算分数是否超过总分
                                            // console.log("满分",Number(content[0].total));
                                            // console.log("总分",Number(rows[0].total)+ Number(score));
                                            // console.log("计算后的分数",Number(rows[0].total)+ Number(score) > Number(content[0].total) );
                                            // 判断分数是否超过满分
                                            if (Number(rows[0].total)+ Number(score) > Number(content[0].total)) {
                                                // 计算超出了多少分
                                                let over_score = Number(rows[0].total)+ Number(score)-Number(content[0].total);
                                                reslove({
                                                    code: 201,
                                                    msg: '总分已经超过满分',
                                                    over_score:over_score
                                                })
                                            }
                                            else {
                                                // 新增
                                                db.query(`INSERT INTO pre_exam_topic (eid, name,question, answer, score,\`option\`) VALUES ('${eid}','${name}','${question}','${answer}','${score}','${option}');`)
                                                    .then(
                                                        content => {
                                                            reslove({
                                                                code: 200,
                                                                msg: "新增题目列表成功",
                                                                status: content.changedRows == 0 ? "正常" : "异常"
                                                            })
                                                        }
                                                    )
                                                    .catch(
                                                        err => {
                                                            reject({
                                                                code: -1,
                                                                msg: "新增失败",
                                                                err
                                                            })
                                                        }
                                                    )
                                            }
                                        })
                                })
                        }
                        else {
                            reslove({
                                cede: 201,
                                msg: "这个题目已经存在"
                            })
                        }
                    }
                )

        })
    }

    // 查找试卷根据eid查找试卷
    topic_list(eid) {
        return new Promise((resolve, reject) => {
            db.query(`SELECT * FROM pre_exam_topic where eid = '${eid}';`)
                .then(
                    row => {
                        if (row.length == 0) {
                            reject({
                                code: -2,
                                msg: `不存在'${eid}'这个eid`
                            })
                        }
                        else {
                            resolve({
                                code: 200,
                                msg: "获取题目列表成功",
                                result: row
                            })
                        }
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: "获取题目列表失败",
                            err
                        })
                    }
                )
        })
    }

}

// 导入考试类
module.exports = Exam;